<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use App\Models\Admin;

class IfLogin
{
	public function handle(Request $request, Closure $next)
	{
		$fyback = session('fyback_info');
		if ($fyback === null) {
			return redirect()->action('Admin\LoginController@index');
		}else{
			$admin = Admin::where('id', '=', $fyback['id'])->first(['sessionid', 'status']);
			switch ($admin['status']) {
				case 2:
					$request->session()->flush(); //清空session
					return redirect()->action('Admin\LoginController@index')->withErrors(['账号已被锁定']);
					break;
				case 3:
					$request->session()->flush();
					return redirect()->action('Admin\LoginController@index')->withErrors(['账号已被禁用']);
					break;
			}
			if ($request->session()->getId() != $admin['sessionid']) {
				$request->session()->flush();
				return redirect()->action('Admin\LoginController@index')->withErrors(['您的账号在另一端登录,如不是本人登陆,请修改密码']);
			}
		}
		return $next($request);
	}
}